package edu.rit.hvz.android.DataLayer;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * This class is responsible for constructing and updating
 * the database as necessary.
 * @author Colton
 *
 */
public class MainDBAdapter {
	public static final String DATABASE_NAME = "hvzDB";
	public static final int DATABASE_VERSION = 1;
	private static final String[] DATABASE_CREATE = {
        "CREATE TABLE player(" + 
        "       _id    		INTEGER PRIMARY KEY, " + 
        "       name  		TEXT," + 
        "       picture		BLOB," +
        "       count 		INTEGER," +
        "       status 		TEXT," +
        "       clan    	INTEGER," +
        "       taggedBy	INTEGER," +	
        "       password 	TEXT," +
        "		FOREIGN KEY(taggedBy) REFERENCES player(_id)," +	
        "		FOREIGN KEY(clan) REFERENCES clan(_id)" +
        ");" ,
        "CREATE TABLE clan(" + 
        "       _id     	INTEGER PRIMARY KEY, " + 
        "       name   		TEXT" +
        ");" ,
        "CREATE TABLE news(" + 
        "       _id    		INTEGER PRIMARY KEY, " + 
        "       title  		TEXT," + 
        "       content 	TEXT," +
        "       visible		BOOLEAN" +
        ");" ,
        "CREATE TABLE mission(" + 
        "       _id    		INTEGER PRIMARY KEY, " + 
        "       title  		TEXT," + 
        "       content		TEXT," +
        "       team 		TEXT," +
        "       visible 	BOOLEAN" +
        ");"
	};
    
    public static void initiateDatabase(Context ctx)
    {
            DatabaseHelper db = new DatabaseHelper(ctx);
           db.getWritableDatabase();
           db.close();
    }
    
    private static class DatabaseHelper extends SQLiteOpenHelper
    {
            DatabaseHelper(Context context)
            {
                    super(context, DATABASE_NAME, null, DATABASE_VERSION);
            }
            @Override
            public void onCreate(SQLiteDatabase db)
            {
                    for(String tbl_creation : DATABASE_CREATE)
                    	db.execSQL(tbl_creation);
            }
            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion,
                            int newVersion)
            {                    
                    db.execSQL("DROP TABLE IF EXISTS titles");
                    onCreate(db);
            }
    }
}
